package com.area.cth.service.impl;

import com.area.cth.mapper.GoodsMapper;
import com.area.cth.pojo.*;
import com.area.cth.service.GoodsService;
import com.area.cth.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper gm;

    @Override
    public void addGoods(Goods g) {
        g.setCreateTime(LocalDateTime.now());
        g.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        g.setCreateUser(id);
        g.setAuditState(1);
        gm.addGoods(g);
    }

    @Override
    public List<Goods> getGoodsList() {
        return gm.getGoodsList();
    }

    @Override
    public List<Goods> getMyGoodsList() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer uid = (Integer) map.get("id");
        return gm.getMyGoodsList(uid);
    }

    @Override
    public List<Goods> searchList(String keyword) {
        return gm.searchList(keyword);
    }

    @Override
    public List<Goods> getGoodsListByCategory(Integer category) {
        return gm.getGoodsListByCategory(category);
    }

    @Override
    public Goods getGoodsById(Integer id) {
        return gm.getGoodsById(id);
    }

    @Override
    @Transactional
    public void deleteGoods(Integer id) {
        //删除收藏表中相关记录
        LambdaQueryWrapper<Collect> collectWrapper = new LambdaQueryWrapper<Collect>()
                .eq(Collect::getGoodsId,id);
        Db.remove(collectWrapper);

        //删除评论表中相关记录
        LambdaQueryWrapper<CommentGoods> commentWrapper=new LambdaQueryWrapper<CommentGoods>()
                .eq(CommentGoods::getGoodsId,id);
        Db.remove(commentWrapper);

        //删除交易表中相关记录
        LambdaQueryWrapper<Deal> dealWrapper=new LambdaQueryWrapper<Deal>()
                .eq(Deal::getGoodsId,id);
        Db.remove(dealWrapper);

        //删除求购表中相关记录
        LambdaQueryWrapper<Manage> manageWrapper=new LambdaQueryWrapper<Manage>()
                .eq(Manage::getGoodsId,id);
        Db.remove(manageWrapper);

        gm.deleteGoods(id);
    }

    @Override
    public void update(Goods g) {
        g.setUpdateTime(LocalDateTime.now());
        g.setAuditState(1);
        gm.update(g);
    }

    @Override
    public List<Goods> getGoodsByUserList(Integer userId) {
        return gm.getGoodsByUserList(userId);
    }

    @Override
    public IPage<Goods> goodsList(Integer pageNo, Integer pageSize, Integer auditState,Integer categoryId, String keyword) {
        Page<Goods> page = Page.of(pageNo, pageSize);
        IPage<Goods> pb = gm.goodsList(page,auditState,categoryId,keyword);
        return pb;
    }
}
